using MES_workbench.Domian.Customers;
using MES_workbench.Domian.Entities;
using NLog.Web;
using SqlSugar;
using System.Threading.Tasks.Dataflow;
using WebApplication1.Implement.IServices;
using WebApplication1.Implement.Services;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//中文注释
builder.Services.AddSwaggerGen(x =>
{
    var file = Path.Combine(AppContext.BaseDirectory + "WebApplication1.xml");
    x.IncludeXmlComments(file, true);
}
);

#region   sqlsugger配置
//创建数据库对象 (用法和EF Dappper一样通过new保证线程安全)
SqlSugarClient Db = new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = builder.Configuration.GetConnectionString("mysql"),
    DbType = DbType.MySql,
    IsAutoCloseConnection = true
},
db => {

    db.Aop.OnLogExecuting = (sql, pars) =>
    {

        //获取原生SQL推荐 5.1.4.63  性能OK
        Console.WriteLine(UtilMethods.GetNativeSql(sql, pars));

        //获取无参数化SQL 对性能有影响，特别大的SQL参数多的，调试使用
        //Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))


    };

    //注意多租户 有几个设置几个
    //db.GetConnection(i).Aop

});

//建库
Db.DbMaintenance.CreateDatabase();//达梦和Oracle不支持建库

//建表（看文档迁移）
Db.CodeFirst.InitTables<WorkOrderProcess>(); //所有库都支持 
Db.CodeFirst.InitTables<WorkReport>(); //所有库都支持 
Db.CodeFirst.InitTables<WorkOrder>(); //所有库都支持
Db.CodeFirst.InitTables<ChatSession>(); //智能客服会话表
Db.CodeFirst.InitTables<ChatMessage>(); //智能客服消息表 

#endregion

#region
//注册上下文：AOP里面可以获取IOC对象，如果有现成框架比如Furion可以不写这一行
builder.Services.AddHttpContextAccessor();
//注册SqlSugar用AddScoped
builder.Services.AddScoped<ISqlSugarClient>(s =>
{
    //Scoped用SqlSugarClient 
    SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
    {
        ConnectionString = builder.Configuration.GetConnectionString("mysql"),
        DbType = DbType.MySql,
        IsAutoCloseConnection = true,
        MoreSettings = new ConnMoreSettings()
        {
            SqlServerCodeFirstNvarchar = true
        }
    },
   db =>
   {
       //每次上下文都会执行

       //获取IOC对象不要求在一个上下文
       //var log=s.GetService<Log>()

       //获取IOC对象要求在一个上下文
       //var appServive = s.GetService<IHttpContextAccessor>();
       //var log= appServive?.HttpContext?.RequestServices.GetService<Log>();

       db.Aop.OnLogExecuting = (sql, pars) =>
       {

       };
   });
    return sqlSugar;
});

#endregion 






// 注册SqlSugarClient
builder.Services.AddScoped<SqlSugarClient>(provider =>
{
    var config = new ConnectionConfig
    {
        ConnectionString = builder.Configuration.GetConnectionString("mysql"),
        DbType = DbType.MySql,
        IsAutoCloseConnection = true,
        MoreSettings = new ConnMoreSettings()
        {
            SqlServerCodeFirstNvarchar = true
        }
    };
    return new SqlSugarClient(config);
});

builder.Services.AddScoped<ICustomerServices, CustomerServices>();
builder.Services.AddHttpClient<IChatService, ChatService>();
builder.Services.AddScoped<IChatService, ChatService>();

// 注册AI服务
builder.Services.AddHttpClient<IAIService, AIService>();
builder.Services.AddScoped<IAIService, AIService>();

builder.Host.UseNLog();




var app = builder.Build();
//跨域
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());

// 添加静态文件支持
app.UseStaticFiles();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseAuthorization();

app.MapControllers();

// 添加默认路由到聊天页面
app.MapFallbackToFile("chat.html");

app.Run();
